package com.lkc.controller;

import com.lkc.bean.Categories;
import com.lkc.bean.Products;
import com.lkc.service.CategoriesService;
import com.lkc.service.ProductsService;
import com.lkc.util.Page;
import com.lkc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("products")
public class ProductsController {

    @Autowired
    private ProductsService productsService;
    @Autowired
    private CategoriesService categoriesService;

    @PostMapping
    public Result add(@RequestBody Products products){
        Object obj = productsService.add(products);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody Products products){
        Object obj = productsService.edit(products);
        return Result.success(obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj = productsService.del(id);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj = productsService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Products products){
        Object obj = productsService.all(products);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Products products, Page page){
        productsService.page(page, products);
        return Result.success(page);
    }
    // 在 ProductsController.java 中添加以下方法
//    @GetMapping("categories")
//    public Result getCategories() {
//        // 调用分类服务获取所有可用分类
//        Categories searchCondition = new Categories();
//        searchCondition.setIsDeleted(0); // 只查询未删除的分类
//        List<Categories> categories = categoriesService.all(searchCondition);
//        return Result.success(categories);
//    }

}
